<?php

class InputController extends Controller
{
	public $defaultAction = 'index';
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		
		if(isset($_FILES['batchFile']) && $_FILES['batchFile']['error']==0)
        {
			$phpExcelPath = Yii::getPathOfAlias('ext.excel');//var_dump($phpExcelPath);die();
			spl_autoload_unregister(array('YiiBase','autoload')); 
			include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');
			$objPHPExcel = PHPExcel_IOFactory::load($_FILES['batchFile']['tmp_name']);          
			$objWorksheet = $objPHPExcel->getActiveSheet(0);  
			spl_autoload_register(array('YiiBase','autoload')); 
 
			$EXCEL_VALUE = array();
			$i = 0;
			foreach ($objWorksheet->getRowIterator() as $row)                                             
			{
				if($i>1)
				{
					$cellIterator = $row->getCellIterator();                                                       
					$cellIterator->setIterateOnlyExistingCells(true);
					foreach ($cellIterator as $cell)
					{
						$EXCEL_VALUE[$i][] = $cell->getValue();
					}
					if(Tender::model()->checkTid($EXCEL_VALUE[$i][0]))
					{
						$tendererid = Tender::model()->getTenderer($EXCEL_VALUE[$i][22]);
						$pid = Tender::model()->getProject($EXCEL_VALUE[$i][1],$tendererid);
						$userid = Tender::model()->getUser($EXCEL_VALUE[$i][6]);
						$tenders = Tender::model()->getTendername($EXCEL_VALUE[$i][2]);//return array{name,rid}
						$mode = Tender::model()->getMode($EXCEL_VALUE[$i][7],$EXCEL_VALUE[$i][9]);
						$insert_value .= "('".$EXCEL_VALUE[$i][0]."',".$pid.",'".$tenders[0]."',".$tenders[1].",".$tendererid.",".$mode."','"
									.$EXCEL_VALUE[$i][14]."','".$EXCEL_VALUE[$i][15]."','".$EXCEL_VALUE[$i][11]."','".$EXCEL_VALUE[$i][12]."',".$EXCEL_VALUE[$i][16].",".$EXCEL_VALUE[$i][17].")";
					}
				}
				$i++;
			}
            var_dump($EXCEL_VALUE); 
		
		}
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
}